/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package engine.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import landapplication.LandApplication;

/**
 *
 */
public class Authentication {
    
    private static Authentication INSTANCE;
    private final String USER_AGENT = "Mozilla/5.0";
    
    public static Authentication getInstance(){
        
        if (INSTANCE == null) {
            
            INSTANCE = new Authentication();
        }
        
        return INSTANCE;
    }

    public void login(String targetURL,String userName, String password) {
        
        try {
            String url = targetURL;

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // optional default is GET
        con.setRequestMethod("GET");

        //add request header
        con.setRequestProperty("User-Agent", USER_AGENT);

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());
        } catch (MalformedURLException ex) {
            Logger.getLogger(LandApplication.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(LandApplication.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
